﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public  class LoginHistoryDBAccess
    {

        /// <summary>
        /// Insert LoginHistory
        /// </summary>
        /// <param name="objLoginHistory"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public bool AddNewLoginHistory(LoginHistory objLoginHistory, String CONNECTION_STRING)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {                
                //new SqlParameter("@LoginHistoryID", objLoginHistory.LoginHistoryID),
                new SqlParameter("@UserID", objLoginHistory.UserID),
                new SqlParameter("@LoginDate", objLoginHistory.LoginDate),
                new SqlParameter("@LogoutDate",objLoginHistory.LogoutDate),
                new SqlParameter("@Descriptions", objLoginHistory.Descriptions),
                new SqlParameter("@SessionId", objLoginHistory.SessionId),
            };

            return SqlDBHelper.ExecuteNonQuery("AddNewLoginHistory", CommandType.StoredProcedure, CONNECTION_STRING, parameters);
        }

        /// <summary>
        /// Update LoginHistory
        /// </summary>
        /// <param name="objLoginHistory"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public bool UpdateLoginHistory(LoginHistory objLoginHistory, String CONNECTION_STRING)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                //new SqlParameter("@LoginHistoryID", objLoginHistory.LoginHistoryID),
                new SqlParameter("@UserID", objLoginHistory.UserID),
                new SqlParameter("@LoginDate", objLoginHistory.LoginDate),
                new SqlParameter("@LogoutDate",objLoginHistory.LogoutDate),
                new SqlParameter("@Descriptions", objLoginHistory.Descriptions),
                new SqlParameter("@SessionId", objLoginHistory.SessionId),
            };

            return SqlDBHelper.ExecuteNonQuery("UpdateLoginHistory", CommandType.StoredProcedure, CONNECTION_STRING, parameters);
        }

        /// <summary>
        /// Delete LoginHistory
        /// </summary>
        /// <param name="LoginHistoryID"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public bool DeleteLoginHistory(int LoginHistoryID, String CONNECTION_STRING)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@LoginHistoryID", LoginHistoryID)
            };

            return SqlDBHelper.ExecuteNonQuery("DeleteLoginHistory", CommandType.StoredProcedure, CONNECTION_STRING, parameters);
        }

        /// <summary>
        /// Get LoginHistory Details
        /// </summary>
        /// <param name="LoginHistoryID"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public LoginHistory GetLoginHistoryDetails(int LoginHistoryID, String CONNECTION_STRING)
        {
            LoginHistory objLoginHistory = null;

            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@LoginHistoryID",LoginHistoryID),
            };
            //Lets get the list of all LoginHistory in a datataable
            using (DataTable table = SqlDBHelper.ExecuteParamerizedSelectCommand("GetLoginHistoryDetails", CommandType.StoredProcedure, CONNECTION_STRING, parameters))
            {
                //check if any record exist or not
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    //Lets go ahead and create the list of LoginHistory
                    objLoginHistory = new LoginHistory();

                    //Now lets populate LoginHistory details into the list of LoginHistory  
                    objLoginHistory.LoginHistoryID = Convert.ToInt32(row["LoginHistoryID"]);                  
                    objLoginHistory.UserID = Convert.ToInt32(row["UserID"]);
                    objLoginHistory.LoginDate = row["LoginDate"].ToString();
                    objLoginHistory.LogoutDate = row["LogoutDate"].ToString();
                    objLoginHistory.Descriptions = row["Descriptions"].ToString();
                    objLoginHistory.SessionId = row["SessionId"].ToString();
                }
            }

            return objLoginHistory;
        }


        /// <summary>
        /// Get LoginHistory List
        /// </summary>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public List<LoginHistory> GetLoginHistoryList(String CONNECTION_STRING)
        {
            List<LoginHistory> listLoginHistory = null;

            //Lets get the list of all LoginHistory in a datataable
            using (DataTable table = SqlDBHelper.ExecuteSelectCommand("GetLoginHistoryList", CommandType.StoredProcedure, CONNECTION_STRING))
            {
                //check if any record exist or not
                if (table.Rows.Count > 0)
                {
                    //Lets go ahead and create the list of LoginHistory
                    listLoginHistory = new List<LoginHistory>();

                    //Now lets populate LoginHistory details into the list of LoginHistory
                    foreach (DataRow row in table.Rows)
                    {
                        LoginHistory objLoginHistory = new LoginHistory();
                        objLoginHistory.LoginHistoryID = Convert.ToInt32(row["LoginHistoryID"]);
                        objLoginHistory.UserID = Convert.ToInt32(row["UserID"]);
                        objLoginHistory.LoginDate = row["LoginDate"].ToString();
                        objLoginHistory.LogoutDate = row["LogoutDate"].ToString();
                        objLoginHistory.Descriptions = row["Descriptions"].ToString();
                        objLoginHistory.SessionId = row["SessionId"].ToString();

                        listLoginHistory.Add(objLoginHistory);
                    }
                }
            }

            return listLoginHistory;
        }        
    }
}
